<?php
/**
 * Class Xu ly thong thong tin loai danh muc
 */
class IndexController extends  Zend_Controller_Action {
	public $baseUrl;
	public function init(){
		//Load cau hinh thu muc trong file config.ini
        $tempDirApp = Zend_Registry::get('conDirApp');
		$this->_dirApp = $tempDirApp->toArray();
		$this->view->dirApp = $tempDirApp->toArray();
		Zend_Loader::loadClass('login_modCheckLogin');		
		//Cau hinh cho Zend_layoutasdfsdfsd
		Zend_Layout::startMvc(array(
			    'layoutPath' => $this->_dirApp['layout'],
			    'layout' => 'logindn'			    
			    ));	
		// Load tat ca cac file Js va Css
		$Efy_Library = new Efy_Library();
		$this->view->LoadAllFileJsCss = $Efy_Library->_getAllFileJavaScriptCss('','efy-js','',',','js');	
		$this->view->showModelDialog = 1;//An menu
		//Lay duong dan thu muc goc (path directory root)
		$this->view->baseUrl = $this->_request->getBaseUrl() . "/public/";
		$this->baseUrl = $this->_request->getBaseUrl() . "/public/";
	}	
	/**
	 * Creater : HUNGVM
	 * Date : 27/09/2009
	 * Idea : Tao phuong thuc hien xu ly logout khoi he thong
	 */
	public function indexAction(){
		//Zend_Loader::loadClass('Zend_Session');
		//Zend_Session::destroy();
		$objInitConfig 		= new Efy_Init_Config();
		$objmodLogin		= new login_modCheckLogin();
		$objInitConst		= new Efy_Init_Const();
		$objRecordFunction	= new Efy_Function_RecordFunctions();
		
		
		$sReURL = $objInitConfig->_setUserLoginUrl();
		$this->view->bodyTitle = 'THÔNG TIN ĐĂNG NHẬP';
		$arrConst =$objInitConst->_setProjectPublicConst();
		$this->view->arrConst= $arrConst;//C-> V(arrConst)
		$hdhOption = $this->_request->getParam('hdn_option');
		$this->view->hdn_option= $hdhOption;
		$sUserName =$this->_request->getParam('txt_usename');//Lay tham so tu V truyen den C
        $sUserName =eregi_replace('[^a-zA-Z0-9_-]','',$sUserName);
		$this->view->txt_usename= $sUserName;
		$sPassWord =$this->_request->getParam('txt_password');
		$this->view->txt_password= $sPassWord;
		$sUrlre= $this->_request->getParam('urlre');
		$this->view->urlRe = $this->baseUrl . "/login/";
		$arrVarSys = $objRecordFunction->getAllObjectbyListCodeFull('', 'DM_THAM_SO_HE_THONG');
		//var_dump($arrVarSys);
		$this->view->onerreportname = $arrVarSys[0]['ONERREPORTNAME'];
		$this->view->loginInfo = $arrVarSys[0]['LOGIN_INFO'];		
		if(isset($_SESSION['staff_id'])){
			$url = $objInitConfig->_getCurrentHttpAndHost();
			if($_SESSION['arrStaffPermission']['RECEIVE']||$_SESSION['arrStaffPermission']['NHOM_TONG_HOP']){
				$url = $url .'record/receive/index/';
				$this->_redirect($url);
			}
			if($_SESSION['arrStaffPermission']['HANDLE']){
				$url = $url .'record/handle/index/';
				$this->_redirect($url);
			}
			if ($_SESSION['STAFF_PERMISSTION'] ==  $objInitConfig->_setPermisstionSystem(1) || $_SESSION['STAFF_PERMISSTION'] ==  $objInitConfig->_setPermisstionSystem(2)){
				$url = $url .'listxml/listtype/index/';
				$this->_redirect($url);
			}
			$this->_redirect($objInitConfig->_setDefaultUrl());
					
		}					
		if($hdhOption =="1"){
			$arrStaff =$objmodLogin->UserCheckLogin($sUserName,md5($sPassWord));//kt username va pass NSD neu dung tra ra mot ban ghi chua tt NSD
			if (sizeof($arrStaff)>0){
				//luu thong tin nguoi dang nhap vao session
				$_SESSION['INFORMATION_STAFF_LOGIN_MTL'] = efy_library::_getInformationStaffLoginMtl($arrStaff[0]['C_NAME'],$arrStaff[0]['C_POSITION_CODE'],$arrStaff[0]['C_UNIT_NAME'],$this->view->baseUrl);
				$_SESSION['staff_id'] = str_replace('{','',str_replace('}', '',$arrStaff[0]['PK_STAFF']));
				$_SESSION['position_name'] = $arrStaff[0]['C_POSITION_CODE'] . ' - ' . $arrStaff[0]['C_NAME'];
				$_SESSION['OWNER_CODE'] = $arrStaff[0]['C_UNIT_OWNER_CODE'];//luu don vi trien khai		
				$_SESSION['OWNER_NAME'] = $arrStaff[0]['C_OWNER_NAME'];
				$_SESSION['STAFF_PERMISSTION'] = $arrStaff[0]['C_ROLE'];
				$_SESSION['STAFF_RECEPTION_DESK'] = $arrStaff[0]['C_RECEPTION_DESK'];					
				//Lay thong tin phong ban
				if(!isset($_SESSION['arr_all_staff']) || is_null($_SESSION['arr_all_staff'])){
					//Luu tru thong tin phong ban cua toan bo cac don vi trien khai
					$_SESSION['arr_all_unit_keep'] = Efy_Init_Session::SesGetDetailInfoOfAllUnit();	
					//Luu co cau to chuc cua can bo hien tai
					$_SESSION['arr_all_unit'] = Efy_Init_Session::_getAllUnitsByCurrentStaff($_SESSION['OWNER_CODE']);	
				}					
				//Lay thong tin can bo
				if(!isset($_SESSION['arr_all_staff']) || is_null($_SESSION['arr_all_staff'])){
					//Luu thong tin can bo cua tat ca don vi trien khai
					$_SESSION['arr_all_staff_keep'] = Efy_Init_Session::SesGetPersonalInfoOfAllStaff();	
					//Luu thong tin can bo thuoc don vi NSD hien thoi
					$_SESSION['arr_all_staff'] = Efy_Init_Session::_getAllUsersByCurrentOrg($_SESSION['OWNER_CODE']);	
				}
				//Lay quyen cua NSD										
				if(!isset($_SESSION['arrStaffPermission']) || is_null($_SESSION['arrStaffPermission'])){
					$_SESSION['arrStaffPermission'] = Efy_Init_Session::StaffPermisionGetAll($_SESSION['staff_id']);			
				}			
				//var_dump($_SESSION['arrStaffPermission']);									 exit;
				//Lay tham so he thong				
				if(!isset($_SESSION['arrSystemParameters']) || is_null($_SESSION['arrSystemParameters'])){
					$arrTemp = Efy_Init_Session::SesGetSystemParameters($_SESSION['OWNER_CODE'],'DM_THAM_SO_HE_THONG');
					$arrDeparmentHandle = Efy_Function_RecordFunctions::getAllObjectbyListCodeFull($_SESSION['OWNER_CODE'],'PHONG_BAN');
					$_SESSION['arrSystemParameters'] = $arrTemp;						
					$_SESSION['lunarHoliday'] 			= 	$arrTemp['lunarHoliday'];
					$_SESSION['solarHoliday'] 			= 	$arrTemp['solarHoliday'];
					$_SESSION['tetHoliday'] 			= 	$arrTemp['tetHoliday'];
					$_SESSION['satTime'] 				= 	$arrTemp['satTime'];
					$_SESSION['am_start_time'] 			= 	$arrTemp['am_start_time'];
					$_SESSION['am_stop_time'] 			= 	$arrTemp['am_stop_time'];
					$_SESSION['pm_start_time'] 			= 	$arrTemp['pm_start_time'];
					$_SESSION['pm_stop_time'] 			= 	$arrTemp['pm_stop_time'];
					$_SESSION['pm_stop_time'] 			= 	$arrTemp['pm_stop_time'];
					//$_SESSION['listRecordTypeCqdb'] 	= 	$arrTemp['listRecordTypeCqdb'];
					$_SESSION['DeparmentHandle']        =   $arrDeparmentHandle;						
				}					
				//Lay thong tin don vi trien khai		
				$_SESSION['SesGetAllOwner'] = Efy_Init_Session::SesGetAllOwner();		
				if (!isset($_SESSION['SesGetAllOwnerNotCurrentOwner'])){
					$arrOwner = array();
					$icount=sizeof($_SESSION['SesGetAllOwner']);
					$arrResult=$_SESSION['SesGetAllOwner'];						
					for($i=0;$i<$icount;$i++){
						if($_SESSION['SesGetAllOwner'][$i]['code']!=$_SESSION['OWNER_CODE']){
							$arr1Owner = array("name"=>$arrResult[$i]['name'],"code"=>$arrResult[$i]['code'],"order"=>$arrResult[$i]['order'],"address"=>$arrResult[$i]['address'],"email"=>$arrResult[$i]['email'],"phone"=>$arrResult[$i]['phone']);
							array_push($arrOwner,$arr1Owner);
						}
					}
					$_SESSION['SesGetAllOwnerNotCurrentOwner'] = $arrOwner;
				}
				// Gan du lieu cho tree
				// $this->setsessiontree();
				$objEfyLib = new Efy_Library();
				$objEfyLib->setsessiontree();
				
				$this->_redirect(Efy_Init_Config::_setDefaultUrl());
			}else{?>
				<script>
					alert('Tên đăng nhập hoặc mật khẩu không đúng!');
				</script><?php
			}	
		}	
	}
}
?>